package actions;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.DeliveryOrderService;
import domain.DeliveryOrder;
/**
 *@author system auto
 */
public class DeliveryOrderCreateAction extends Action {
	/**
	 * 订单service.
	 */
	private DeliveryOrderService deliveryOrderService = null;
	/**
	 * 创建订单.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
	    // 验证是否是有效的Token,防止重复提交
	    if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	        return mapping.findForward("error.duplicateSubmit");
	     }
	    try{
	        DynaValidatorForm deliveryOrderForm = (DynaValidatorForm) form;
	        DeliveryOrder deliveryOrder = new DeliveryOrder();
	        this.convertBean(deliveryOrder,deliveryOrderForm);
	        deliveryOrderService.addDeliveryOrder(deliveryOrder);
	      }catch(Exception e){
			ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	        // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	      }
	      return mapping.findForward("success");
	}
	/**
	 *convertBean
	 */
	 private void convertBean(DeliveryOrder deliveryOrder, DynaValidatorForm deliveryOrderForm)
	 		throws Exception{
	    	String orderNumber=deliveryOrderForm.getString("orderNumber");
	        if(orderNumber!=null&&!orderNumber.equals("")){
	        	deliveryOrder.setOrderNumber(orderNumber);
	        }
	    	String orderType=deliveryOrderForm.getString("orderType");
	        if(orderType!=null&&!orderType.equals("")){
	        	deliveryOrder.setOrderType(orderType);
	        }
	    	String vipCustomFlag=deliveryOrderForm.getString("vipCustomFlag");
	        if(vipCustomFlag!=null&&!vipCustomFlag.equals("")){
	        	deliveryOrder.setVipCustomFlag(vipCustomFlag);
	        }
	    	String sender=deliveryOrderForm.getString("sender");
	        if(sender!=null&&!sender.equals("")){
	        	deliveryOrder.setSender(sender);
	        }
	    	String fetchAddr=deliveryOrderForm.getString("fetchAddr");
	        if(fetchAddr!=null&&!fetchAddr.equals("")){
	        	deliveryOrder.setFetchAddr(fetchAddr);
	        }
	    	String senderPhone=deliveryOrderForm.getString("senderPhone");
	        if(senderPhone!=null&&!senderPhone.equals("")){
	        	deliveryOrder.setSenderPhone(senderPhone);
	        }
	    	String receiver=deliveryOrderForm.getString("receiver");
	        if(receiver!=null&&!receiver.equals("")){
	        	deliveryOrder.setReceiver(receiver);
	        }
	    	String receiverAddr=deliveryOrderForm.getString("receiverAddr");
	        if(receiverAddr!=null&&!receiverAddr.equals("")){
	        	deliveryOrder.setReceiverAddr(receiverAddr);
	        }
	    	String receiverPhone=deliveryOrderForm.getString("receiverPhone");
	        if(receiverPhone!=null&&!receiverPhone.equals("")){
	        	deliveryOrder.setReceiverPhone(receiverPhone);
	        }
	    	String originLocation=deliveryOrderForm.getString("originLocation");
	        if(originLocation!=null&&!originLocation.equals("")){
	        	deliveryOrder.setOriginLocation(originLocation);
	        }
	    	String destLocation=deliveryOrderForm.getString("destLocation");
	        if(destLocation!=null&&!destLocation.equals("")){
	        	deliveryOrder.setDestLocation(destLocation);
	        }
	    	String chargeWeight=deliveryOrderForm.getString("chargeWeight");
	        if(chargeWeight!=null&&!chargeWeight.equals("")){
	        	deliveryOrder.setChargeWeight(Double.valueOf(chargeWeight));
	        }
	    	String dimension=deliveryOrderForm.getString("dimension");
	        if(dimension!=null&&!dimension.equals("")){
	        	deliveryOrder.setDimension(dimension);
	        }
	    	String contentsDesc=deliveryOrderForm.getString("contentsDesc");
	        if(contentsDesc!=null&&!contentsDesc.equals("")){
	        	deliveryOrder.setContentsDesc(contentsDesc);
	        }
	    	String payType=deliveryOrderForm.getString("payType");
	        if(payType!=null&&!payType.equals("")){
	        	deliveryOrder.setPayType(payType);
	        }
	    	String specialOperateNote=deliveryOrderForm.getString("specialOperateNote");
	        if(specialOperateNote!=null&&!specialOperateNote.equals("")){
	        	deliveryOrder.setSpecialOperateNote(specialOperateNote);
	        }
	    	String insuranceFee=deliveryOrderForm.getString("insuranceFee");
	        if(insuranceFee!=null&&!insuranceFee.equals("")){
	        	deliveryOrder.setInsuranceFee(Double.valueOf(insuranceFee));
	        }
	    	String agencyReceiveFee=deliveryOrderForm.getString("agencyReceiveFee");
	        if(agencyReceiveFee!=null&&!agencyReceiveFee.equals("")){
	        	deliveryOrder.setAgencyReceiveFee(Double.valueOf(agencyReceiveFee));
	        }
	    	String orderDate=deliveryOrderForm.getString("orderDate");
	        if(orderDate!=null&&!orderDate.equals("")){
	        	DateFormat DATE_FAMAT_ORDERDATE = new SimpleDateFormat("yyyy-MM-dd",Locale.UK);
	        	deliveryOrder.setOrderDate(new Timestamp(DATE_FAMAT_ORDERDATE.parse(orderDate).getTime()));
	        }
	    	String goodsName=deliveryOrderForm.getString("goodsName");
	        if(goodsName!=null&&!goodsName.equals("")){
	        	deliveryOrder.setGoodsName(goodsName);
	        }
	    	String goodsPrice=deliveryOrderForm.getString("goodsPrice");
	        if(goodsPrice!=null&&!goodsPrice.equals("")){
	        	deliveryOrder.setGoodsPrice(Double.valueOf(goodsPrice));
	        }
	    	String serviceType=deliveryOrderForm.getString("serviceType");
	        if(serviceType!=null&&!serviceType.equals("")){
	        	deliveryOrder.setServiceType(serviceType);
	        }
	    	String subStation=deliveryOrderForm.getString("subStation");
	        if(subStation!=null&&!subStation.equals("")){
	        	deliveryOrder.setSubStation(subStation);
	        }
	    	String creater=deliveryOrderForm.getString("creater");
	        if(creater!=null&&!creater.equals("")){
	        	deliveryOrder.setCreater(creater);
	        }
	    	String fetcher=deliveryOrderForm.getString("fetcher");
	        if(fetcher!=null&&!fetcher.equals("")){
	        	deliveryOrder.setFetcher(fetcher);
	        }
	    	String dispatcher=deliveryOrderForm.getString("dispatcher");
	        if(dispatcher!=null&&!dispatcher.equals("")){
	        	deliveryOrder.setDispatcher(dispatcher);
	        }
	    	String problemCode=deliveryOrderForm.getString("problemCode");
	        if(problemCode!=null&&!problemCode.equals("")){
	        	deliveryOrder.setProblemCode(problemCode);
	        }
	    	String orderStatus=deliveryOrderForm.getString("orderStatus");
	        if(orderStatus!=null&&!orderStatus.equals("")){
	        	deliveryOrder.setOrderStatus(orderStatus);
	        }
	    	String scheduler=deliveryOrderForm.getString("scheduler");
	        if(scheduler!=null&&!scheduler.equals("")){
	        	deliveryOrder.setScheduler(scheduler);
	        }
	    	String orderNote=deliveryOrderForm.getString("orderNote");
	        if(orderNote!=null&&!orderNote.equals("")){
	        	deliveryOrder.setOrderNote(orderNote);
	        }
	    	String deliveryFee=deliveryOrderForm.getString("deliveryFee");
	        if(deliveryFee!=null&&!deliveryFee.equals("")){
	        	deliveryOrder.setDeliveryFee(Double.valueOf(deliveryFee));
	        }
	    	String pkgCount=deliveryOrderForm.getString("pkgCount");
	        if(pkgCount!=null&&!pkgCount.equals("")){
	        	deliveryOrder.setPkgCount(Integer.valueOf(pkgCount));
	        }
	 }
	/**
     * @param pDeliveryOrderService the deliveryOrderService to set
     */
    public void setDeliveryOrderService(DeliveryOrderService pDeliveryOrderService) {
        deliveryOrderService = pDeliveryOrderService;
    }
}
